﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace System.Windows.Forms
{
    public static class ExtensionsMethodsForThreads
    {
        private delegate void SetTextCallBack(Control ctrl, string value);
        private delegate string GetTextCallBack(Control ctrl);

        private delegate void SetBooleanCallBack(Control ctrl, bool value);
        private delegate bool GetBooleanCallBack(Control ctrl);

        private delegate void SetBooleanFormCallBack(Form ctrl, bool value);
        private delegate bool GetBooleanFormCallBack(Form ctrl);

        private delegate object GetSelectedItemCallBack(ComboBox ctrl);
        private delegate void AddItemCallBack(ComboBox ctrl, object value);

        private delegate ProgressBarStyle GetProgressBarStyleCallBack(ProgressBar ctrl);
        private delegate void SetProgressBarStyleCallBack(ProgressBar ctrl, ProgressBarStyle value);

        public static void SetText(this Control ctrl, string value)
        {
            if (ctrl.InvokeRequired)
            {
                SetTextCallBack d = new SetTextCallBack(SetText);
                ctrl.FindForm().Invoke(d, new object[] { ctrl, value });
            }
            else
            {
                ctrl.Text = value;
            }
        }
        public static string GetText(this Control ctrl)
        {
            if (ctrl.InvokeRequired)
            {
                GetTextCallBack d = new GetTextCallBack(GetText);
                return ctrl.FindForm().Invoke(d, new object[] { ctrl }).ToString();
            }
            else
            {
                return ctrl.Text;
            }
        }

        public static void SetEnabled(this Control ctrl, bool value)
        {
            if (ctrl.InvokeRequired)
            {
                SetBooleanCallBack d = new SetBooleanCallBack(SetEnabled);
                ctrl.FindForm().Invoke(d, new object[] { ctrl, value });
            }
            else
            {
                ctrl.Enabled = value;
            }
        }
        public static bool GetEnabled(this Control ctrl)
        {
            if (ctrl.InvokeRequired)
            {
                GetTextCallBack d = new GetTextCallBack(GetText);
                return (bool)ctrl.FindForm().Invoke(d, new object[] { ctrl });
            }
            else
            {
                return ctrl.Enabled;
            }
        }

        public static void SetVisible(this Control ctrl, bool value)
        {
            if (ctrl.InvokeRequired)
            {
                SetBooleanCallBack d = new SetBooleanCallBack(SetVisible);
                ctrl.FindForm().Invoke(d, new object[] { ctrl, value });
            }
            else
            {
                ctrl.Visible = value;
            }
        }
        public static bool GetVisible(this Control ctrl)
        {
            if (ctrl.InvokeRequired)
            {
                GetBooleanCallBack d = new GetBooleanCallBack(GetVisible);
                return (bool)ctrl.FindForm().Invoke(d, new object[] { ctrl });
            }
            else
            {
                return ctrl.Visible;
            }
        }

        public static void SetControlBox(this Form ctrl, bool value)
        {
            if (ctrl.InvokeRequired)
            {
                SetBooleanFormCallBack d = new SetBooleanFormCallBack(SetControlBox);
                ctrl.FindForm().Invoke(d, new object[] { ctrl, value });
            }
            else
            {
                ctrl.ControlBox = value;
            }
        }
        public static bool GetControlBox(this Form ctrl)
        {
            if (ctrl.InvokeRequired)
            {
                GetBooleanFormCallBack d = new GetBooleanFormCallBack(GetControlBox);
                return (bool)ctrl.FindForm().Invoke(d, new object[] { ctrl });
            }
            else
            {
                return ctrl.ControlBox;
            }
        }

        public static object GetSelectedItem(this ComboBox ctrl)
        {
            if (ctrl.InvokeRequired)
            {
                GetSelectedItemCallBack d = new GetSelectedItemCallBack(GetSelectedItem);
                return ctrl.FindForm().Invoke(d, new object[] { ctrl });
            }
            else
            {
                return ctrl.SelectedItem;
            }
        }
        public static void AddItem(this ComboBox ctrl, object value)
        {
            if (ctrl.InvokeRequired)
            {
                AddItemCallBack d = new AddItemCallBack(AddItem);
                ctrl.FindForm().Invoke(d, new object[] { ctrl, value });
            }
            else
            {
                ctrl.Items.Add(value);
            } 
        }

        public static ProgressBarStyle GetProgressBarStyle(this ProgressBar ctrl)
        {
            if (ctrl.InvokeRequired)
            {
                SetProgressBarStyleCallBack d = new SetProgressBarStyleCallBack(SetProgressBarStyle);
                return (ProgressBarStyle)ctrl.FindForm().Invoke(d, new object[] { ctrl});
            }
            else
            {
                return ctrl.Style;
            }
        }
        public static void SetProgressBarStyle(this ProgressBar ctrl, ProgressBarStyle value)
        {
            if (ctrl.InvokeRequired)
            {
                SetProgressBarStyleCallBack d = new SetProgressBarStyleCallBack(SetProgressBarStyle);
                ctrl.FindForm().Invoke(d, new object[] { ctrl, value });
            }
            else
            {
                ctrl.Style = value;
            }
        }
    }
}
